<?php
/**
 * Main application controller
 *
 * @author Lukas Normantas
 */
class UserController extends Areal_Controller_Action
{
    public function adminAction()
    {
        $current_page = $this->_getParam('page');
        $filter = new Default_Model_UserAdminFilter();
        $sort = $this->getRequest()->getParam('sort');
        if ($sort){
            $filter->setSort($sort);
            return $this->_helper->redirector('admin','user','default',
                                                  array('page'=>$current_page));
        }
        $this->view->assign('users', $filter->getAdminPaginator($current_page));
    }

    public function addAction()
    {
        $user = new Default_Model_User();
        if ($this->getRequest()->isPost()){
            if ($user->edit($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('user saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin');
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('User not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                $this->view->form = $user->getUserForm();
            }
        } else{
            $this->view->form = $user->getUserForm();
        }
    }

    public function editAction()
    {
        $user_id = $this->getRequest()->getParam('user_id');
        $user = Doctrine::getTable('Default_Model_User')->find($user_id);
        if ($this->getRequest()->isPost()){
            if ($user->edit($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('User saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin');
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('User not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        } 
        $this->view->form = $user->getUserForm();
        
    }

    public function deleteAction()
    {
        $user_id = $this->getRequest()->getParam('user_id');
        $form = new Default_Form_ConfirmForm();
        if ($this->getRequest()->isPost()){
            if ($form->isValid($this->getRequest()->getPost())){
                $user = Doctrine::getTable('Default_Model_User')->find($user_id);
                $user->delete();
                $message = Zend_Registry::get('Zend_Translate')->translate('User deleted');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin');
            }
        }
        $this->view->form = $form;
    }

    public function registerAction()
    {
        $user = new Default_Model_User();
        if ($this->getRequest()->isPost()){
            if ($user->register($this->getRequest()->getPost(), $this->view)){
                $message = Zend_Registry::get('Zend_Translate')->translate('Welcome registered');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('awaiting-registration-confirm');
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('User was not regirested, correct mistakes and try again');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->view->form = $user->getRegisterForm();
    }

    public function awaitingRegistrationConfirmAction()
    {
    }

    public function confirmRegistrationAction()
    {
        $user_id = $this->getRequest()->getParam('user_id');
        $registration_code = $this->getRequest()->getParam('registration_code');
        if ( Default_Model_User::confirmRegistration($user_id, $registration_code) ){
            $message = Zend_Registry::get('Zend_Translate')->translate(
                'Your registration is confirmed');
            $this->getHelper('flashMessenger')->addMessage($message);
            return $this->_helper->redirector('index', 'index', 'default');
        } else {
            $message = Zend_Registry::get('Zend_Translate')->translate(
                'Your registration was not confirmed, contact us to help you.');
            $this->getHelper('flashMessenger')->addMessage($message);
            return $this->_helper->redirector('awaiting-registration-confirm');
        }
    }

    public function editProfileAction()
    {
        $user_id = Zend_Auth::getInstance()->getIdentity()->getUserId();
        $user = Doctrine::getTable('Default_Model_User')->find($user_id);
        if ($this->getRequest()->isPost()){
            if ($user->editProfile($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Your profile saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('edit-profile');
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Your profile not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        } 
        $this->view->form = $user->getProfileForm();
        
    }

    public function loginAction()
    {
        $user = new Default_Model_User();
        if ($this->getRequest()->isPost()){
            $post = $this->getRequest()->getPost();
            if (isset($post['login_attempted_from_request_params'])){
                if ($user->login($post)){
                    $message = Zend_Registry::get('Zend_Translate')->translate('Welcome Back');
                    $this->getHelper('flashMessenger')->addMessage($message);
                } else {
                    $message = Zend_Registry::get('Zend_Translate')->translate('Can not login');
                    $this->getHelper('flashMessenger')->addMessage($message);
                }
                $r = unserialize($post['login_attempted_from_request_params']);
                $module = $r['module']; unset($r['module']);
                $controller = $r['controller']; unset($r['controller']);
                $action = $r['action']; unset($r['action']);
                return $this->_helper->redirector($action,$controller,$module,$r);
            }
        }
        $this->view->identity = Zend_Auth::getInstance()->getIdentity();
        $this->view->form = $user->getLoginForm();
    }

    public function adminLoginAction()
    {
        $user = new Default_Model_User();
        if ($this->getRequest()->isPost()){
            $post = $this->getRequest()->getPost();
   
            if ($user->login($post)){
                $message = Zend_Registry::get('Zend_Translate')->translate('Welcome Back');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin', 'user', 'default');
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Can not login');
                $this->getHelper('flashMessenger')->addMessage($message);
            }

     
        }
        $this->view->identity = Zend_Auth::getInstance()->getIdentity();
        $this->view->form = $user->getLoginForm();
    }








    public function logoutAction(){
        Zend_Auth::getInstance()->clearIdentity();
        return $this->_helper->redirector('index','index','default');
    }
    
    public function forgotPasswordAction()
    {
        $user = new Default_Model_User();
        if ($this->getRequest()->isPost()){
            if ($user->grantPasswordChange($this->getRequest()->getPost(), $this->view)){
                $message = Zend_Registry::get('Zend_Translate')->translate('Password change granted, check your email');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('index', 'index', 'default');
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('No such email');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->view->form = $user->getEmailForm();
         
    }

    public function changeLostPasswordAction()
    {
        $user_id = $this->getRequest()->getParam('user_id');
        $code = $this->getRequest()->getParam('code');
        $user = Doctrine::getTable('Default_Model_User')->find($user_id);
        if ( Default_Model_User::isPasswordChangeGranted($user_id, $code) ){
            if ($this->getRequest()->isPost()){
                if ($user->editLostPassword($this->getRequest()->getPost())){
                    $message = Zend_Registry::get('Zend_Translate')->translate('Your new password saved');
                    $this->getHelper('flashMessenger')->addMessage($message);
                    return $this->_helper->redirector('index', 'index', 'default');
                } else {
                    $message = Zend_Registry::get('Zend_Translate')->translate('Your new password not saved');
                    $this->getHelper('flashMessenger')->addMessage($message);
                }
            }
            $this->view->form = $user->getLostPasswordForm();
        } else {
            $message = Zend_Registry::get('Zend_Translate')->translate(
                'Your password cannot be changed, contact us to help you.');
            $this->getHelper('flashMessenger')->addMessage($message);
            return $this->_helper->redirector('login');
        }
    }

    public function changePasswordAction()
    {
        if ( ! Zend_Auth::getInstance()->hasIdentity()){
            $message = Zend_Registry::get('Zend_Translate')->translate(
                'You must be logged in');
            $this->getHelper('flashMessenger')->addMessage($message);
            return $this->_helper->redirector('index', 'index', 'default');
        }
        $user_id = Zend_Auth::getInstance()->getIdentity()->getUserId();
        $user = Doctrine::getTable('Default_Model_User')->find($user_id);
        if ($this->getRequest()->isPost()){
            if ($user->changePassword($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('Your password changed');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('index', 'index', 'default');
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Your password not changed');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->view->form = $user->getPasswordChangeForm();
    }

    public function changeEmailAction()
    {
        if ( ! Zend_Auth::getInstance()->hasIdentity()){
            $message = Zend_Registry::get('Zend_Translate')->translate(
                'You must be logged in');
            $this->getHelper('flashMessenger')->addMessage($message);
            return $this->_helper->redirector('index', 'index', 'default');
        }
        $user_id = Zend_Auth::getInstance()->getIdentity()->getUserId();
        $user = Doctrine::getTable('Default_Model_User')->find($user_id);
        if ($this->getRequest()->isPost()){
            if ($user->grantEmailChange($this->getRequest()->getPost(), $this->view)){
                $message = Zend_Registry::get('Zend_Translate')->translate('Email confirm link was sent to your new email');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('index', 'index', 'default');
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('Email can not be changed');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        }
        $this->view->form = $user->getNewEmailForm();
    }

    public function confirmNewEmailAction(){
        $user_id = $this->getRequest()->getParam('user_id');
        $code = $this->getRequest()->getParam('code');
        if ( Default_Model_User::confirmNewEmail($user_id, $code) ){
            $message = Zend_Registry::get('Zend_Translate')->translate(
                'Your email was changed');
            $this->getHelper('flashMessenger')->addMessage($message);
        } else {
            $message = Zend_Registry::get('Zend_Translate')->translate(
                'Your email was not changed, contact us to help you.');
            $this->getHelper('flashMessenger')->addMessage($message);
        }
        return $this->_helper->redirector('index', 'index', 'default');
    }

    public function updatePasswordAction()
    {
        $user_id = $this->getRequest()->getParam('user_id');
        $user = Doctrine::getTable('Default_Model_User')->find($user_id);
        if ($this->getRequest()->isPost()){
            if ($user->updatePassword($this->getRequest()->getPost())){
                $message = Zend_Registry::get('Zend_Translate')->translate('User password saved');
                $this->getHelper('flashMessenger')->addMessage($message);
                return $this->_helper->redirector('admin');
            } else {
                $message = Zend_Registry::get('Zend_Translate')->translate('User password not saved');
                $this->getHelper('flashMessenger')->addMessage($message);
            }
        } 
        $this->view->form = $user->getUpdatePasswordForm();
    }


}